package trycat.cn.pagelistview;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {

    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private List<String> mTitleList = new ArrayList<>();

    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mFragments;//适配器的数据源
    private Fragment managerFragment, signCardFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.content_main);


        mViewPager = (ViewPager) findViewById(R.id.customViewPager);
        mTabLayout = (TabLayout) findViewById(R.id.tabs);
//        headerLL = getBinding().headerLL;
//        headerIV = getBinding().headerIV;

        mTitleList.add("打卡");
        mTitleList.add("管理");

        mTabLayout.addTab(mTabLayout.newTab());//添加tab选项卡
        mTabLayout.addTab(mTabLayout.newTab());


        mFragments = new ArrayList<>();//初始化数据源
        signCardFragment = SignCardFragment.newInstance();
        managerFragment = ManagerFragment.newInstance();
        mFragments.add(signCardFragment);
        mFragments.add(managerFragment);

        //初始化适配器
        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override
            public CharSequence getPageTitle(int position) {
                return mTitleList.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }

            @Override
            public Fragment getItem(int arg0) {
                return mFragments.get(arg0);
            }
        };
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(0);
        mTabLayout.setupWithViewPager(mViewPager);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                List<String> list = new ArrayList();
                for (int i = 0; i < 10; i++) {
                    list.add("Header" + i);
                }
                ((SignCardFragment) signCardFragment).setData(list);
            }
        }, 500);


    }
}
